/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.model.entity.behaviours;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.geom.Rectangle;

import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.equipment.EquipmentModel;
import ca.usherbrooke.model.equipment.ProtectionModel;
import ca.usherbrooke.model.equipment.WeaponModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class EquipmentBehaviour extends EntityBehaviour {
	private static final long serialVersionUID = 1L;

	private final Integer maxCapacity;

	private final ArrayList<EquipmentModel> equipements;

	public static Boolean canPickUpEquipment(IEntityModel entity,
			EquipmentModel newEquipement) {
		boolean intersect = false;

		// verify if we do not contain it
		List<EquipmentModel> equipements = entity.getEquipments();

		// verify if the entity already contain the equipment
		boolean canBeHold = !equipements.contains(newEquipement);
		// verify if the equipment is not hold by an entity
		canBeHold = canBeHold && newEquipement.getHolder() == null;
		// verify if the entity have enough space to take the new equipment
		canBeHold = canBeHold
				&& entity.getCapacity() + newEquipement.getCapacity() <= entity
						.getMaxCapacity();

		// verify if we intersect the object
		if (newEquipement != null) {
			Rectangle entitySize = entity.getSize();
			Rectangle equipment = newEquipement.getSize();
			intersect = entitySize.intersects(equipment)
					|| entitySize.contains(equipment)
					|| equipment.contains(entitySize);
		}

		return intersect && canBeHold;
	}

	public EquipmentBehaviour(IEntityModel model, Integer maxCapacity) {
		super(model);

		this.maxCapacity = maxCapacity;
		equipements = new ArrayList<EquipmentModel>();
	}

	public List<EquipmentModel> getEquipments() {
		return this.equipements;
	}

	public Boolean isHoldEquipements() {
		return this.equipements.size() > 0;
	}

	public Boolean canPickUpEquipment(EquipmentModel newEquipement) {
		return canPickUpEquipment(this.entity, newEquipement);
	}

	public void pickUp(EquipmentModel newEquipement) {
		if (canPickUpEquipment(newEquipement)) {
			newEquipement.setHolder(entity);
			equipements.add(newEquipement);
		}
	}

	public Boolean putDown(EquipmentModel oldEquipement) {
		Boolean isPutDown = false;

		if (equipements.contains(oldEquipement)) {
			equipements.remove(oldEquipement);

			if (oldEquipement != null
					&& oldEquipement.getHolder() == this.entity) {
				isPutDown = true;
				oldEquipement.setHolder(null);
				oldEquipement.setPosition(entity.getPosition());
			}
		}

		return isPutDown;
	}

	public List<WeaponModel> getWeapons() {
		List<WeaponModel> result = new ArrayList<WeaponModel>();
		for (EquipmentModel equipment : equipements) {
			if (equipment instanceof WeaponModel) {
				result.add((WeaponModel) equipment);
			}
		}
		return result;
	}

	public List<ProtectionModel> getProtections() {
		List<ProtectionModel> result = new ArrayList<ProtectionModel>();
		for (EquipmentModel equipment : equipements) {
			if (equipment instanceof ProtectionModel) {
				result.add((ProtectionModel) equipment);
			}
		}
		return result;
	}

	public Integer getCapacity() {
		Integer capacity = 0;
		for (EquipmentModel equipment : equipements) {
			capacity += equipment.getCapacity();
		}

		return capacity;
	}

	public Integer getMaxCapacity() {
		return maxCapacity;
	}
}
